package com.goudan.ojcloudserviceclient.FeignClient;

import com.goudan.ojcloudserviceclient.fallback.UserFallBack;
import com.goudan.ojcommon.common.ErrorCode;
import com.goudan.ojcommon.exception.BusinessException;

import com.goudan.ojmodel.entity.User;
import com.goudan.ojmodel.enums.UserRoleEnum;

import com.goudan.ojmodel.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

import static com.goudan.ojcommon.constant.UserConstant.USER_LOGIN_STATE;


/**
 * 用户服务
 */
@FeignClient(name = "oj-cloud-user-service",path = "/api/user/inner",fallback = UserFallBack.class)
public interface UserFeignClient {
    /**
     * 根据id获取用户
     * @param userId
     * @return
     */
    @GetMapping("/get/id")
    User getById(@RequestParam("userId") Long userId);

    /**
     * 根据id列表获取用户列表
     * @param idList
     * @return
     */
    @GetMapping("/get/ids")
    List<User> listByIds(@RequestParam("idList") Collection<Long> idList);
    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
   default User getLoginUser(HttpServletRequest request){
       // 先判断是否已登录
       Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
       User currentUser = (User) userObj;
       if (currentUser == null || currentUser.getId() == null) {
           throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
       }
       // 可以考虑在这里做全局权限校验
       return currentUser;

   }

    /**
     * 获取脱敏的用户信息
     *
     * @param user
     * @return
     */
   default UserVO getUserVO(User user){
       if (user == null) {
           return null;
       }
       UserVO userVO = new UserVO();
       BeanUtils.copyProperties(user, userVO);
       return userVO;
   }

    /**
     * 是否为管理员
     *
     * @param user
     * @return
     */
    default boolean isAdmin(User user){
        return user != null && UserRoleEnum.ADMIN.getValue().equals(user.getUserRole());
    }
}
